function logout() {
    if (confirm("是否注销当前登录？")) {
        deleteCookie('___USER_TOKEN');
        window.top.location.href = '/isys/login.html';
    }
};
$(function () {

    var user_token = getCookie('___USER_TOKEN');
    if (user_token) {
        var tips = "<div class='u_user_name'><span class='u_label' style='width:70px;'>登录账户：</span><a id='logininfo' href='./main.html' title='点击进入'>加载中...</a><br/><a href='#' onclick='logout();'>账户注销</a></div>";
        $("#loginForm").html(tips);
        $.ajax({
            url: '/services/api/account/user/userinfo',
            type: "GET",
            dataType: "json",
            success: function (data) {
                $("#logininfo").html(data.nickName);
            }
        });
    } else {
        var memberpassword = getCookie("memberpassword");
        if (memberpassword == "1") {
            $("#userName").val(getCookie("userName"));
            $("#password").val(getCookie("password"));
        } else if (memberpassword == "0") {
            $("#userName").val(getCookie("userName"));
        }
    }

    $("i[class^=u_icon-checkbox]").click(function () {
        $("i[class^=u_icon-checkbox]").toggleClass("u_icon-checkbox-checked");
    });


    $.extend(window.Locale["en"], {
        "errorMsg_loginName_or_password_is_invalid": "LoginName or password is invalid!"
    });
    $.extend(window.Locale["zh-cn"], {
        "errorMsg_loginName_or_password_is_invalid": "用户名或密码错误！"

    });

    $('#loginForm').submit(submitForm(function (e, actions) {
        var deviceId = getCookie(_config.device_id_name);
        if (deviceId == null || deviceId == '' || deviceId == 'undefined') {
            actions.push(function (next) {
                $.ajax({url: '/services/api/common/device/reg', type: 'post',
                    data: JSON.stringify(genDeviceData()),
                    success: function (re) {
                        setCookie(_config.device_id_name, re.id);
                        next();
                    }, error: function () {
                        next();
                    }
                });
            });
        }
        actions.push(function (next) {
            deleteCookie('___USER_TOKEN');
            deleteCookie("CTRL_DEVICE_ID");
            var deviceId = getCookie(_config.device_id_name);
            var expireTime = new Date().getTime() + 1000 * 3600 * 24 * 30;
            var userName = $('#userName').val();
            var data = "{'deviceId':'" + deviceId + "','loginName':'" + $('#userName').val() + "', 'password':'" + $('#password').val() + "', 'expireTime':" + expireTime + "}";
            $.ajax({
                url: '/services/api/account/user/login',
                type: "POST",
                data: data,
                dataType: "json",
                success: function (data) {
                    var memberpassword = $("#memberpassword").attr("class");
                    if (memberpassword.indexOf("u_icon-checkbox-checked") >= 0) {
                        setCookie("login_memberpassword", "1");
                        setCookie("login_data", $('#userName').val() + "@" + $('#password').val());
                    } else {
                        setCookie("login_memberpassword", "0");
                        setCookie("login_data", $('#userName').val() + "@");
                    }
                    setCookie(_config.user_token_name, data['sessionFullStr']);
                    console.log(data);
                    window.top.location.href = './main.html';
                }
            });
        }.bind(this));
    }));
});


//判断是否选中记住密码的框，如果选中则页面打开的时候也默认选中
function rememberPass() {
    var memberpassword = getCookie("login_memberpassword");
    if (memberpassword == "1") {
        var userData = getCookie("login_data").split("@");
        $("#memberpassword").addClass("u_icon-checkbox-checked");
        $("#userName").val(userData[0]);
        $("#password").attr("type", "password");
        $("#password").val(userData[1]);
    } else if (memberpassword == "0") {
        var userData = getCookie("login_data").split("@");
        ;
        $("#userName").val(userData[0]);
        $("#password").val(userData[1]);
    } else {
        $("#userName").val("");
        $("#password").val("");
    }


}

//页面初始化
$(document).ready(function () {
    rememberPass();
});


